package com.han.hotelplat.comm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.han.hotelplat.comm.ResponseWrapTo;
import com.han.hotelplat.comm.ResponseWrapTo.CodeType;
import com.han.hotelplat.comm.utils.AreaHelp;
import com.han.hotelplat.model.AreaModel;

@Controller
@RequestMapping("/area")
public class AreaController {

	@ResponseBody
	@RequestMapping(value = "getprovinces", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String getProvinces(){
		AreaModel[] provinces = AreaHelp.getInstance().getChinaProvinces();
		return new ResponseWrapTo(CodeType.success,"查询成功",provinces).toJson();
	}
	
	@ResponseBody
	@RequestMapping(value = "getChildArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String  getChildArea(String code){
		AreaModel areaModel = AreaHelp.getInstance().getAreaModel(code);
		AreaModel[] childs=areaModel==null?null:areaModel.getChilds();
		if(childs==null) {
			childs=new AreaModel[0];
			return new ResponseWrapTo<AreaModel[]>(CodeType.noData, "没有找到该地域下的子区域",childs ).toJson();
		}else {
			return new ResponseWrapTo<AreaModel[]>(CodeType.success, "ok",childs).toJson();
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "getAllArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String  getAllArea(String code){
		AreaModel areaModel = AreaHelp.getInstance().getAreaModel(code);
		AreaModel[] childs=areaModel==null?null:areaModel.getChilds();
		AreaModel parents=areaModel==null?null:areaModel.getParent();
		if(childs==null) {
			childs=new AreaModel[0];
			return new ResponseWrapTo<AreaModel[]>(CodeType.noData, "没有找到该地域下的子区域",childs ).toJson();
		}else {
			return new ResponseWrapTo<AreaModel[]>(CodeType.success, "ok",childs).toJson();
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "getParentsArea", produces = "application/json;charset=UTF-8", method = { RequestMethod.GET })
	public String  getParentsArea(String code){
		AreaModel areaModel = AreaHelp.getInstance().getAreaModel(code);
		if(areaModel==null) {
			return new ResponseWrapTo<AreaModel>(CodeType.noData, "没有找到该地域下的子区域",areaModel ).toJson();
		}else {
			return new ResponseWrapTo<AreaModel>(CodeType.success, "ok",areaModel).toJson();
		}
	}
}
